package com.hdcx.Minfresh.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hdcx.Minfresh.R;
import com.hdcx.Minfresh.bean.GoodListWrapper;
import com.hdcx.Minfresh.bean.UserInfoBean;
import com.hdcx.Minfresh.url.MyUrl;
import com.hdcx.Minfresh.util.ShpfUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/11/18.
 */

public class MineFeedbackActivity extends BaseActivity {
    Button bt_upload;
    EditText ed_content;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_feedback);
        initView();
        initData();
    }

    private void initView() {
        bt_upload = (Button) findViewById(R.id.bt_upload);
        ed_content = (EditText) findViewById(R.id.ed_content);
        bt_upload.setOnClickListener(this);
        ((TextView) findViewById(R.id.tv_title)).setText("反馈意见");
        findViewById(R.id.tv_left).setOnClickListener(this);
    }

    private void initData() {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.bt_upload:
                post_upload();
                break;
        }
    }
  /*  参数
            uid   //用户id
    msg	  //建议内容*/

    private void post_upload() {
        if (ed_content.getText().toString().trim().equals("")) {
            Toast.makeText(MineFeedbackActivity.this, "请输入内容！", Toast.LENGTH_SHORT).show();
            return;
        }
        JSONObject json = new JSONObject();
        UserInfoBean user = ShpfUtil.getObject(ShpfUtil.LOGIN);
        try {
            json.put("uid", user.getUserinfo().getId());
            json.put("msg", ed_content.getText().toString().trim());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OkHttpUtils.postString()
                .url(MyUrl.FEEDBACK)
                .content(json.toString())
                .build()
                .execute(new Callback<GoodListWrapper>() {
                    @Override
                    public GoodListWrapper parseNetworkResponse(Response response) throws IOException {
                        String json = response.body().string();
                        return new Gson().fromJson(json, GoodListWrapper.class);
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                    }

                    @Override
                    public void onResponse(GoodListWrapper response) {
                        if (response.getStatus() == 1) {
                            Toast.makeText(MineFeedbackActivity.this, "提交成功！", Toast.LENGTH_SHORT).show();
                            finish();
                        }
                    }
                });
    }
}
